<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="评价" prop="star">
				<el-select v-model="queryParams.star" clearable size="small" placeholder="请选择评价">
					<el-option :label="'差评'" :value="1"></el-option>
					<el-option :label="'中评'" :value="3"></el-option>
					<el-option :label="'好评'" :value="5"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-table v-loading="loading" :data="evaluateList" @selection-change="handleSelectionChange">
			<el-table-column type="index" :index="indexMethod" label="序号" width="55" align="center" />
			<el-table-column label="评价" align="center">
				<template slot-scope="scope">
					<el-rate disabled v-model="scope.row.star" :texts="texts" show-text></el-rate>
				</template>
			</el-table-column>
			<el-table-column label="评价内容" :show-overflow-tooltip="true" width="200" align="center" prop="content" />
			<el-table-column label="医生回复" :show-overflow-tooltip="true" width="200" align="center" prop="comment" />
			<el-table-column label="患者姓名" align="center">
				<template slot-scope="scope">{{ scope.row.patient.patientName }}</template>
			</el-table-column>
			<el-table-column label="评价时间" align="center" prop="createTime" />
			<div slot="empty">
				<el-empty description="暂无信息"></el-empty>
			</div>
		</el-table>

		<pagination
			v-show="total>0"
			:total="total"
			:page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize"
			@pagination="getList"
		/>
	</div>
</template>

<script>
import { listEvaluate, getEvaluate, delEvaluate, addEvaluate, updateEvaluate, exportEvaluate } from "@/api/hospital/evaluate";

export default {
	name: "Evaluate",
	props: {
		doctorId: {}
	},
	data() {
		return {
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 评价表格数据
			evaluateList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				star: null,
				doctorId: null
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
			},
			texts: ['差评', '', '中评', '', '好评']
		};
	},
	methods: {
		/** 查询评价列表 */
		getList() {
			this.loading = true;
			listEvaluate(this.queryParams).then(response => {
				this.evaluateList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				evaluateId: null,
				star: null,
				content: null,
				comment: null,
				doctorId: null,
				orderId: null,
				patientId: null,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null
			};
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.evaluateId)
			this.single = selection.length!==1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = "添加评价";
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			const evaluateId = row.evaluateId || this.ids
			getEvaluate(evaluateId).then(response => {
				this.form = response.data;
				this.open = true;
				this.title = "修改评价";
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.evaluateId != null) {
						updateEvaluate(this.form).then(response => {
							if (response.code === 200) {
								this.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							}
						});
					} else {
						addEvaluate(this.form).then(response => {
							if (response.code === 200) {
								this.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							}
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const evaluateIds = row.evaluateId || this.ids;
			this.$confirm('是否确认删除评价编号为"' + evaluateIds + '"的数据项?', "警告", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(function() {
				return delEvaluate(evaluateIds);
			}).then(() => {
				this.getList();
				this.msgSuccess("删除成功");
			}).catch(function() {});
		},
		/** 导出按钮操作 */
		handleExport() {
			const queryParams = this.queryParams;
			this.$confirm('是否确认导出所有评价数据项?', "警告", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(function() {
				return exportEvaluate(queryParams);
			}).then(response => {
				this.download(response.msg);
			}).catch(function() {});
		},
		indexMethod(index) {
			return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
		}
	},
	watch: {
		doctorId: function() {
			this.queryParams.doctorId = this.doctorId
			this.getList();
		}
	}
};
</script>
